import { Dialog } from 'cube-ui'
let dialog
export function showAlert (content, icon) {
  return new Promise(resolve => {
    dialog = Dialog.$create({
      type: 'alert',
      title: content,
      content: '',
      icon: icon || 'cubeic-alert',
      onConfirm: () => {
        resolve()
      }
    }).show()
  })
}

export function showPrompt (title) {
  return new Promise((resolve, reject) => {
    dialog = Dialog.$create({
      type: 'prompt',
      title: title,
      prompt: {
        value: null,
        placeholder: '请输入'
      },
      onConfirm: (e, promptValue) => {
        resolve(promptValue)
        dialog = Dialog.$create()
      }
    })
    dialog.show()
  })
}

export function showConfirm (title, content) {
  return new Promise(resolve => {
    dialog = Dialog.$create({
      type: 'confirm',
      icon: 'cubeic-alert',
      title: title,
      content: content,
      confirmBtn: {
        text: '确定',
        active: true,
        disabled: false,
        href: 'javascript:;'
      },
      cancelBtn: {
        text: '取消',
        active: false,
        disabled: false,
        href: 'javascript:;'
      },
      onConfirm: () => {
        resolve(true)
      },
      onCancel: () => {
        resolve(false)
      }
    }).show()
  })
}

export function showCorrect (content) {
  return showAlert(content, 'cubeic-ok')
}

export function hide () {
  dialog.hide()
}
